package com.yanqu.road.server.cmd.kuang.battle;

import com.yanqu.road.entity.kuang.KuangConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.kuang.Kuang;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.kuang.KuangModule;
import com.yanqu.road.server.manger.kuang.KuangMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_KUANG_LEAVE_BATTLE, desc = "离开pvp")
public class KuangBattleLeaveBattleCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        Kuang.KuangBattleLeaveReqMsg reqMsg = Kuang.KuangBattleLeaveReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        KuangConfig config = KuangMgr.getConfig();
        if (config == null) {
            ret = GameErrorCode.E_KUANG_CONFIG_NOT_FOUND;
            return;
        }
        ret = player.getModule(KuangModule.class).leaveBattle();
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            Kuang.KuangBattleLeaveRespMsg.Builder respMsg = Kuang.KuangBattleLeaveRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_KUANG_LEAVE_BATTLE, respMsg);
        }
    }
}
